/*
 * EasyCamera
 *
 * Copyright (c) 2020.  Frezrik frezrik@126.com
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package com.camera.cameratest01;

import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;

import com.camera.EasyUvcCamera;
import com.camera.widget.YuvSurfaceView;
import com.frezrik.common.thread.ThreadPoolManager;

import java.util.HashMap;
import java.util.TreeSet;
import java.util.concurrent.ExecutorService;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private int VIS_PID;
    private int VIS_VID;
    private int NIS_PID;
    private int NIS_VID;
    private int mDgrees;
    private boolean mNeedMirror;

    private TextView mTv;
    private YuvSurfaceView mSvLeft;
    private YuvSurfaceView mSvRight;
    private EasyUvcCamera mCameraVis;
    private EasyUvcCamera mCameraNis;
    private ExecutorService mPool;

    private void getCamera() {
        //读这个属性
        //"persist.system.cameramodel"

        // 云从
        /*VIS_PID = 0xC053;
        VIS_VID = 0x0C45;
        NIS_PID = 0xB051;
        NIS_VID = 0x0C45;
        mDgrees = 270;
        mNeedMirror = true;*/

        // 华捷
        /*VIS_PID = 515;
        VIS_VID = 11707;
        mDgrees = 0;
        mNeedMirror = false;*/

        // 奥比
        VIS_PID = 1294;
        VIS_VID = 11205;
        mDgrees = 0;
        mNeedMirror = false;

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dual);

        printUSB();

        getCamera();

        mTv = findViewById(R.id.tv);

        mSvLeft = findViewById(R.id.sv_left);
        mSvRight = findViewById(R.id.sv_right);

        mSvLeft.setResolution(640, 480);
        mSvLeft.setDisplayOrientation(mDgrees, mNeedMirror);

        mSvRight.setResolution(640, 480);
        mSvRight.setDisplayOrientation(mDgrees, mNeedMirror);

        mCameraVis = new EasyUvcCamera();
        mCameraVis.initUvcCamera(this, VIS_VID, VIS_PID);

        mCameraNis = new EasyUvcCamera();
        mCameraNis.initUvcCamera(this, NIS_VID, NIS_PID);

        mPool = ThreadPoolManager.newCachedThreadPool(4);
        /*mPool.execute(() -> {
            while (!Thread.currentThread().isInterrupted()) {
                mSvLeft.setNV21Data(mCameraVis.updateFrame());
            }
        });*/

        /*mPool.execute(() -> {
            while (!mCameraVis.isUvcCameraAttach()) {
                SystemClock.sleep(500);
            }
            mCameraNis.openCamera();
            mCameraVis.openCamera();
        });*/

        //mCameraVis.setFrameStreamFormat(UVCCamera.FRAME_FORMAT_YUYV);
        mCameraVis.openCamera();

        mCameraVis.setPreviewCallback((data) -> {
            //Log.d("zmzm", "setPreviewCallbackL:");
            mSvLeft.setYUV420spData(data);
        });

        mCameraNis.openCamera();
        mCameraNis.setPreviewCallback((data) -> {
            Log.d("zmzm", "setPreviewCallbackR:");
            mSvRight.setYUV420spData(data);
        });
        /*mPool.execute(() -> {
            while (!Thread.currentThread().isInterrupted()) {
                mSvLeft.setNV21Data(mCameraNis.updateFrame());
            }
        });*/

    }

    private void printUSB() {
        UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
        if (usbManager != null) {
            HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
            if (deviceList != null) {
                TreeSet<String> usbDevices = new TreeSet<>();
                for(UsbDevice device : deviceList.values()) {
                    usbDevices.add(device.getProductName() + "[" + device.getVendorId() + ", " + device.getProductId() + "]");
                }
                int cnt = 0;
                for(String usbDevice : usbDevices) {
                    Log.e("getDeviceList", "UsbDevice[" + cnt++ + "]: " + usbDevice);
                }
            }
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        mCameraVis.startStream();
        mCameraNis.startStream();

    }

    @Override
    protected void onStop() {
        super.onStop();
        mCameraVis.stopStream();
        mCameraNis.stopStream();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        mPool.shutdownNow();

        mCameraVis.closeCamera();
        mCameraNis.closeCamera();
    }
}
